package com.ys.peony.view;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.megvii.idcardlib.util.Util;
import com.ys.peony.R;
import com.ys.peony.car300.ItemModel;
import com.ys.peony.utils.PixelUtils;
import com.ys.peony.utils.YsUtil;
import com.ys.peony.view.inter.YsDailogClickListener;

import java.util.ArrayList;
import java.util.List;

import com.yklab.library.refreshlayout.adapters.CommonRecyclerViewAdapter;
import com.yklab.library.refreshlayout.holder.CommonRecyclerViewHolder;
import com.yklab.library.refreshlayout.inter.OnClickCommonListener;

/**
 * Created by 59153 on 2017/8/3.
 */

public class YsDialogItemModel {
    private Dialog mDialog;
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mManager;
    private CommonRecyclerViewAdapter mAdapter;
    private int mType;
    private Context mContext;
    private List<ItemModel> mItemModels;
    private Window mWindow;
    private YsDailogClickListener mYsDailogClickListener;

    public YsDialogItemModel(Context context, YsDailogClickListener listener) {
        mContext = context;
        mYsDailogClickListener = listener;
        init();
    }

    private void init() {
        mItemModels = new ArrayList<>();
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_dialog_list, null, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        mManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mAdapter = new CommonRecyclerViewAdapter<ItemModel>(mContext, mItemModels, R.layout.view_dialog_list_item) {
            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                ((CommonRecyclerViewHolder) holder).setText(R.id.tv_item, mItemModels.get(position).getText().toString().trim());
            }
        };
        mAdapter.isHasFooter(false);
        mAdapter.setOnClickListener(new OnClickCommonListener() {
            @Override
            public void onClickListener(View view, int position) {
                dismiss();
                if (mYsDailogClickListener != null && position < mItemModels.size()) {
                    mYsDailogClickListener.clickDialogItemModel(mType, mItemModels.get(position));
                }
            }
        });
        mRecyclerView.setLayoutManager(mManager);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setHasFixedSize(true);
        mDialog = new Dialog(mContext, R.style.dialog);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(true);
        mWindow = mDialog.getWindow();
        mWindow.setGravity(Gravity.BOTTOM);
        mWindow.getAttributes().windowAnimations = R.style.dialog_animation;
    }

    public void setItemModels(List<ItemModel> itemModels, int type) {
        if (itemModels != null && itemModels.size() > 0) {
            if (itemModels.size() > 4) {
                mWindow.setLayout(YsUtil.getScreenWidth(mContext), PixelUtils.dp2px(200));
            } else {
                mWindow.setLayout(YsUtil.getScreenWidth(mContext), PixelUtils.dp2px(50 * itemModels.size()));
            }
            show();
            mType = type;
            mItemModels.clear();
            mItemModels.addAll(itemModels);
            mAdapter.notifyDataSetChanged();

        }
    }

    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    public void dismiss() {
        mDialog.dismiss();
    }
}
